package com.heiyu.task;

import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.heiyu.utils.DateUtils;
import com.heiyu.utils.KlineUtils;
import org.apache.commons.io.FileUtils;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//@Component
public class KlineTask {

    private ExecutorService service = Executors.newFixedThreadPool(10);

    @Scheduled(fixedRate = 864000)
    public void execute(){
            File file = new File("/Users/mac/Desktop/kline");
            readFile(file);
    }

    private void readFile(File file){
        if(file.isDirectory()){
            File[] list = file.listFiles();
            for(File f:list){
               readFile(f);
            }
        }else{
            if(file.getName().endsWith("_5m.json")&&file.getName().indexOf(DateUtils.format(DateUtils.getTargetDate(new Date(),-1),"yyyyMMdd"))>=0){
                service.execute(new KlineJob(file));
            }
        }
    }
}

class KlineJob implements Runnable{

    private File file;

    public KlineJob(File file){
        this.file = file;
    }

    @Override
    public void run() {
        String obj = null;
        try {
            obj = FileUtils.readFileToString(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(obj != null){
            obj = "["+obj.replaceAll("\n",",")+"]";
            JSONArray dayData = JSONArray.parseArray(obj);
            JSONArray line1;
            JSONArray line2;
            JSONArray line3;
            StringBuilder sb  = new StringBuilder();
            List<BigDecimal> decimals;
            for(int i=0;i<dayData.size();i++){
                if(i+2>=dayData.size()){
                    line1 = JSONArray.parseArray(dayData.get(i).toString());
                    sb.append("[").append(line1.get(0)).append(",").append(line1.get(1)).append(",").append(line1.get(2)).append(",");
                    sb.append(line1.get(3)).append(",");
                    sb.append(line1.get(4)).append(",");
                    sb.append(line1.get(5)).append("]\n");
                }else{
                    line1 =JSONArray.parseArray(dayData.get(i).toString());
                    line2 = JSONArray.parseArray(dayData.get(i+1).toString());
                    line3 = JSONArray.parseArray(dayData.get(i+2).toString());
                    sb.append("[").append(line3.get(0)).append(",").append(line1.get(1)).append(",").append(line3.get(2)).append(",");
                    decimals = Lists.newArrayList();
                    decimals.add(new BigDecimal(line1.get(3).toString()));
                    decimals.add(new BigDecimal(line2.get(3).toString()));
                    decimals.add(new BigDecimal(line3.get(3).toString()));
                    sb.append(KlineUtils.min(decimals)).append(",");
                    decimals = Lists.newArrayList();
                    decimals.add(new BigDecimal(line1.get(4).toString()));
                    decimals.add(new BigDecimal(line2.get(4).toString()));
                    decimals.add(new BigDecimal(line3.get(4).toString()));
                    sb.append(KlineUtils.max(decimals)).append(",");
                    decimals = Lists.newArrayList();
                    decimals.add(new BigDecimal(line1.get(5).toString()));
                    decimals.add(new BigDecimal(line2.get(5).toString()));
                    decimals.add(new BigDecimal(line3.get(5).toString()));
                    sb.append(KlineUtils.count(decimals)).append("]\n");
                }
                i=i+2;
            }
            String fileName = file.getPath().substring(0,file.getPath().indexOf("_5m"));
            try {
                FileUtils.writeStringToFile(new File(fileName+"_15m.json"),sb.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
